import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { VM } from "db://oops-framework/libs/model-view/ViewModel";

/** 设置 数据层 */
@ecs.register('PlayerInfoModel')
export class PlayerInfoModelComp extends ecs.Comp {
    /** 提供 MVVM 组件使用的数据 */
    public vm: any = {
        nickName: "",
        avatarUrl: ""
    };

    /** 显示数据添加到 MVVM 框架中监视 */
    vmAdd() {
        VM.add(this.vm, "PlayerInfoModel");
    }

    /** 显示数据从 MVVM 框架中移除 */
    vmRemove() {
        VM.remove("PlayerInfoModel");
    }

    /** 用户id */
    public _playerId: string = '1';
    /** 用户头像 */
    public _avatar: string = '1';
    /** 用户名称 */
    public _name: string = '1';

    public get playerId(): string {
        return this._playerId;
    }

    public set playerId(v: string) {
        this._playerId = v;
        this.vm["playerId"] = `ID：${v}`;
    }

    public get avatar(): string {
        return this._avatar;
    }

    public set avatar(v: string) {
        if (!v) {
            v = `gui/main/texture/head`;
        }
        this._avatar = v;
        this.vm["avatar"] = v;
        this.vm["avatarUrl"] = v;
    }

    public get name(): string {
        return this._name;
    }

    public set name(v: string) {
        this._name = v;
        this.vm["name"] = v;
        this.vm["nickName"] = v;
    }

    /** 数据层组件移除时，重置所有数据为默认值 */
    reset() {
        for (var key in this.vm) {
            delete this.vm[key];
        }
    }
}